<?php

use yii\helpers\Html;
use yii\widgets\ActiveForm;
use kartik\select2\Select2;
use yii\helpers\ArrayHelper;
use backend\models\Contractor;
use backend\models\FinanceSource;
use backend\models\Contract;
use backend\models\ContractStatus;
use backend\models\ContractPurchaseMethod;
use backend\models\ContractPurpose;
use backend\models\Donor;
use backend\models\Project;
use backend\models\OrganizationBranch;
use Da\User\Model\User;
use backend\helpers\ActionButton;

/* @var $this yii\web\View */
/* @var $model backend\models\Contract */
/* @var $form yii\widgets\ActiveForm */
?>

<div class="create-form contract-form">

    <?php $form = ActiveForm::begin(); ?>
    <?= $form->field($model, 'registry_number')->textInput(['readonly' => true]) ?>

    <div class="row">
        <?= $form->field($model, 'own_number', ['options' => ['class' => 'col-sm-4']])->textInput() ?>
        <?= $form->field($model, 'contractor_number', ['options' => ['class' => 'col-sm-4']])->textInput() ?>
        <?= $form->field($model, 'id_status', ['options' => ['class' => 'col-sm-4']])->dropdownList(
            ContractStatus::find()->select(['title', 'id'])->indexBy('id')->orderBy('sort')->column(),
            [
                'prompt' => 'Выберете стутус договора...'
            ])
        ?>
    </div>


    <div class="row">
        <?= $form->field($model, 'id_contractor', ['options' => ['class' => 'col-sm-10']])->widget(Select2::classname(), [
                'data' => Contractor::contractorList(),
                'options' => ['placeholder' => 'Выберете контрагента...'],
            ]); 
        ?>
        <div class="col-sm-2">
            <label class="control-label">&nbsp;</label>
            <?= ActionButton::custom('Добавить контрагента', 'createContractor', 'contractor/create', null, 'btn-primary', false, '_blank') ?>
        </div>
    </div>
    
    <?= $form->field($model, 'subject')->textInput(['maxlength' => true]) ?>

    <div class="row">
        <?= $form->field($model, 'id_finance_source', ['options' => ['class' => 'col-sm-4']])->dropdownList(
                FinanceSource::financeSourceList(),
                [
                    'prompt' => 'Выберете источник финансирования...'
                ]
            )
        ?>
        <?= $form->field($model, 'id_purchase_method', ['options' => ['class' => 'col-sm-4']])->dropdownList(
            ContractPurchaseMethod::find()->select(['title', 'id'])->indexBy('id')->column(),
            [
                'prompt' => 'Выберете способ закупки...'
            ])
        ?>
        <?= $form->field($model, 'id_purpose', ['options' => ['class' => 'col-sm-4']])->dropdownList(
            ContractPurpose::find()->select(['title', 'id'])->indexBy('id')->column(),
            [
                'prompt' => 'Выберете цель закупки...'
            ])
        ?>
    </div>

    <?= $form->field($model, 'total_price')->textInput(['maxlength' => true]) ?>

    <div class="row">
        <?= $form->field($model, 'id_donor', ['options' => ['class' => 'col-sm-6']])->widget(Select2::classname(), [
                'data' => Donor::find()->select(['name', 'id'])->indexBy('id')->column(),
                'options' => ['placeholder' => 'Выберете донора...'],
            ]); 
        ?>
        <?= $form->field($model, 'id_project', ['options' => ['class' => 'col-sm-6']])->widget(Select2::classname(), [
                'data' => Project::find()->select(['title', 'id'])->where(['!=', 'status', Project::STATUS_PLANNED])->indexBy('id')->column(),
                'options' => ['placeholder' => 'Выберете проект...'],
            ]); 
        ?>
    </div>
    
    <div class="row">
        <?= $form->field($model, 'date_sign', ['options' => ['class' => 'col-sm-3']])->input('date')  ?>
        <?= $form->field($model, 'date_realize', ['options' => ['class' => 'col-sm-3']])->input('date') ?>
        <?= $form->field($model, 'is_yearly', ['options' => ['class' => 'col-sm-3']])->radioList(
        [
            Contract::ONE_TIME => 'Нет',
            Contract::YEAR_TIME => 'Да',
        ])?>
    </div>

    <?= $form->field($model, 'id_liable_user')->widget(Select2::classname(), [
            'data' => ArrayHelper::map(User::find()->with('profile')->all(), 'id', 'profile.name'),
            'options' => ['placeholder' => 'Выберите сотрудника...'],
        ]); 
    ?>

    <?= $form->field($model, 'id_organization_branch')->dropdownList(
            OrganizationBranch::find()->select(['title', 'id'])->indexBy('id')->column(),
            [
                'prompt' => 'Выберете филиал...'
            ]
        );
    ?>

    <?= $form->field($model, 'comment')->textarea(['rows' => 6]) ?>

    <br>
    
    <h2 class="main-title">Документооборот - первый этап</h2>

    <!-- <div class="row">
        <?= $form->field($model, 'docKpRequestOne', ['options' => ['class' => 'col-sm-4']])->fileInput(['accept' => '.doc,.docx,.pdf,.zip']) ?>
        <div class="col-sm-8">
            <?php 
            if(!empty($model->doc_kp_request_one))
            {
                echo '<label class="control-label">Запрос коммерческого предложения №1</label>';
                echo '<div class="anchor-container">';
                echo Html::a($model->doc_kp_request_one, 'https://storage.yandexcloud.net/' . Contract::BUCKET_NAME  . '/' . $model->doc_kp_request_one);
                echo '</div>';
            }
            ?>
        </div>
    </div>

    <div class="row">
        <?= $form->field($model, 'docKpRequestTwo', ['options' => ['class' => 'col-sm-4']])->fileInput(['accept' => '.doc,.docx,.pdf,.zip']) ?>
        <div class="col-sm-8">
            <?php 
            if(!empty($model->doc_kp_request_two))
            {
                echo '<label class="control-label">Запрос коммерческого предложения №2</label>';
                echo '<div class="anchor-container">';
                echo Html::a($model->doc_kp_request_two, 'https://storage.yandexcloud.net/' . Contract::BUCKET_NAME  . '/' . $model->doc_kp_request_two);
                echo '</div>';
            }
            ?>
        </div>
    </div> -->

    <div class="row">
        <?= $form->field($model, 'docKpAnswerOne', ['options' => ['class' => 'col-sm-4']])->fileInput(['accept' => '.doc,.docx,.pdf,.zip']) ?>
        <div class="col-sm-8">
            <?php 
            if(!empty($model->doc_kp_answer_one))
            {
                echo '<label class="control-label">Коммерческое предложение №1</label>';
                echo '<div class="anchor-container">';
                echo Html::a($model->doc_kp_answer_one, 'https://storage.yandexcloud.net/' . Contract::BUCKET_NAME  . '/' . $model->doc_kp_answer_one);
                echo '</div>';
            }
            ?>
        </div>
    </div>

    <div class="row">
        <?= $form->field($model, 'docKpAnswerTwo', ['options' => ['class' => 'col-sm-4']])->fileInput(['accept' => '.doc,.docx,.pdf,.zip']) ?>
        <div class="col-sm-8">
            <?php 
            if(!empty($model->doc_kp_answer_two))
            {
                echo '<label class="control-label">Коммерческое предложение №2</label>';
                echo '<div class="anchor-container">';
                echo Html::a($model->doc_kp_answer_two, 'https://storage.yandexcloud.net/' . Contract::BUCKET_NAME  . '/' . $model->doc_kp_answer_two);
                echo '</div>';
            }
            ?>
        </div>
    </div>

    <div class="row">
        <?= $form->field($model, 'docKpScreenshot', ['options' => ['class' => 'col-sm-4']])->fileInput(['accept' => '.doc,.docx,.pdf,.zip']) ?>
        <div class="col-sm-8">
            <?php 
            if(!empty($model->doc_kp_screenshot))
            {
                echo '<label class="control-label">Специальная версия КП (скриншоты)</label>';
                echo '<div class="anchor-container">';
                echo Html::a($model->doc_kp_screenshot, 'https://storage.yandexcloud.net/' . Contract::BUCKET_NAME  . '/' . $model->doc_kp_screenshot);
                echo '</div>';
            }
            ?>
        </div>
    </div>

    <br>

    <h2 class="main-title">Документооборот - второй этап</h2>

    <div class="row">
        <?= $form->field($model, 'docMainContract', ['options' => ['class' => 'col-sm-4']])->fileInput(['accept' => '.doc,.docx,.pdf,.zip']) ?>
        <div class="col-sm-8">
            <?php 
            if(!empty($model->doc_main_contract))
            {
                echo '<label class="control-label">Договор</label>';
                echo '<div class="anchor-container">';
                echo Html::a($model->doc_main_contract, 'https://storage.yandexcloud.net/' . Contract::BUCKET_NAME  . '/' . $model->doc_main_contract);
                echo '</div>';
            }
            ?>
        </div>
    </div>

    <br>

    <h2 class="main-title">Документооборот - третий этап</h2>

    <div class="row">
        <?= $form->field($model, 'docInvoice', ['options' => ['class' => 'col-sm-4']])->fileInput(['accept' => '.doc,.docx,.pdf,.zip']) ?>
        <div class="col-sm-8">
            <?php 
            if(!empty($model->doc_invoice))
            {
                echo '<label class="control-label">Счёт</label>';
                echo '<div class="anchor-container">';
                echo Html::a($model->doc_invoice, 'https://storage.yandexcloud.net/' . Contract::BUCKET_NAME  . '/' . $model->doc_invoice);
                echo '</div>';
            }
            ?>
        </div>
    </div>

    <div class="row">
        <?= $form->field($model, 'docClose', ['options' => ['class' => 'col-sm-4']])->fileInput(['accept' => '.doc,.docx,.pdf,.zip']) ?>
        <div class="col-sm-8">
            <?php 
            if(!empty($model->doc_close))
            {
                echo '<label class="control-label">Закрывающий документ</label>';
                echo '<div class="anchor-container">';
                echo Html::a($model->doc_close, 'https://storage.yandexcloud.net/' . Contract::BUCKET_NAME  . '/' . $model->doc_close);
                echo '</div>';
            }
            ?>
        </div>
    </div>

    <div class="row">
        <?= $form->field($model, 'docPaymentOrder', ['options' => ['class' => 'col-sm-4']])->fileInput(['accept' => '.doc,.docx,.pdf,.zip']) ?>
        <div class="col-sm-8">
            <?php 
            if(!empty($model->doc_payment_order))
            {
                echo '<label class="control-label">Платёжное поручение</label>';
                echo '<div class="anchor-container">';
                echo Html::a($model->doc_payment_order, 'https://storage.yandexcloud.net/' . Contract::BUCKET_NAME  . '/' . $model->doc_payment_order);
                echo '</div>';
            }
            ?>
        </div>
    </div>

    <br>

    <div class="form-group">
        <?= Html::submitButton('Сохранить', ['class' => 'btn btn-success']) ?>
    </div>

    <?php ActiveForm::end(); ?>

</div>
